#ifndef RENDERWIDGET_H
#define RENDERWIDGET_H

#include<QIcon>
#include<QWidget>
#include<QString>
#include <QPainter>
#include <QPixmap>
#include<QPainter>
#include<QPaintEvent>
#include<QPixmap>
#include<QSvgRenderer>
#include<QDebug>

class RenderWidget:public QWidget
{
    Q_OBJECT
private:
//    const QString& m_img;
    QPixmap m_img;
    QSvgRenderer* renderer;
    QString type;

public:
    RenderWidget(const QString& filepath){
        resize(200,200);
//        QString str = filepath.fi
        int nodeindx;
        QString str;
        qDebug() << "构造";
        if((nodeindx=filepath.indexOf("."))!=-1)
        {
            str = filepath.mid(nodeindx+1);
            qDebug() << "str = " << str;
            if("png" == str)
            {
                type = "png";
                qDebug() << "png type";
                m_img = filepath;
            }
            else if("svg" == str)
            {
                type = "svg";
                qDebug() << "svg type";
                renderer = new QSvgRenderer(filepath);
            }
            else
            {
                qDebug() << "未知图片类型";
            }
        }
    }
    ~RenderWidget(){}
public:
    void paintEvent(QPaintEvent* event) override {
        QPainter painter(this);
        if(type == "png")
        {
            QPixmap scaledPixmap = m_img.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
            painter.drawPixmap(0,0,scaledPixmap);
        }
        else if(type == "svg" && renderer)
        {
            renderer->render(&painter,this->rect());
        }
    }
};

#endif
